'use strict'

let 
    form = $('#test-form'),
    langs = form.find('[name=lang]'),
    selectAll = form.find('label.selectAll:checkbox'),
    selectAllLabel = form.find('span.selectAll'),
    deselectAllLabel = form.find('span.deselectAll'),
    invertSelect = form.find('a.invertSelect');

    langs.click(function(){
        if(langs.not(':checked').length===0){
            selectAll.prop('checked',true);
            selectAllLabel.css('display','none');
            deselectAllLabel.css('display','inline');
        }else{
            selectAll.prop('checked',false);
            selectAllLabel.css('display','inline');
            deselectAllLabel.css('display','none');
        }
    })

    selectAll.click(function(){
        if(selectAll.is()){
            langs.prop(':checked',true);
            selectAllLabel.css('display','none');
            deselectAllLabel.css('display','inline');
        }else{
            langs.prop(':checked',false);
            selectAllLabel.css('display','inline');
            deselectAllLabel.css('display','none');
        }
    })

    invertSelect.click(function(){
        langs.map(function(){
            let ch = $(this);

            if(ch.prop('checked')){
                ch.prop('checked',false);
            }else{
                ch.prop('checked',true);
            }
        })
        if(langs.not(':checked').length===langs.length){
            selectAll.prop('checked',false);
        }else if(langs.not(':checked').length===0){
            selectAll.prop('checked',true);
        }
    })